/*
 * @lc app=leetcode.cn id=1249 lang=javascript
 *
 * [1249] 移除无效的括号
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string}
 */
var minRemoveToMakeValid = function(s) {
  let leftDel = [];
  let rightDel = [];
  for(let i in s) {
      if (s[i] === '(') {
          leftDel.push(i);
      } else if (s[i] === ')') {
          if (leftDel.length) {
              leftDel.pop();
          } else {
              rightDel.push(i);
          }
      }
  }
  const del = [...leftDel, ...rightDel];
  const res = [...s];
  for(let i = 0; i < del.length; i++) {
      res[del[i]] = '';
  }
  return res.join('');
};
// @lc code=end

